package router

import (
	"api/config"
	"api/controller"
	"api/global"
	"api/middleware"

	docs "api/docs"

	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
)

func SetupRouter(config *config.AppConfig) *gin.Engine {
	if config.Mode == gin.ReleaseMode {
		gin.SetMode(gin.ReleaseMode)
	}
	r := gin.New()
	r.Use(middleware.GinLogger(global.Logger), middleware.GinRecovery(global.Logger, true), middleware.Cors())

	r.POST("/api/user/login", controller.Login)
	r.GET("/api/captcha", controller.GetCaptcha)

	group := r.Group("/api")
	group.Use(middleware.Login())
	{
		userGroup := group.Group("/user")
		{
			userGroup.POST("/re_token", controller.Users)

			userGroup.GET("/list", controller.Users)
			userGroup.POST("/add", controller.Users)
			userGroup.PUT("/update", controller.Users)
			userGroup.DELETE("/delete", controller.Users)

			roleGroup := userGroup.Group("/role")
			roleGroup.GET("/get", controller.Users)
			roleGroup.POST("/add", controller.Users)
			roleGroup.PUT("/update", controller.Users)
			roleGroup.DELETE("/delete", controller.Users)
			roleGroup.GET("/list", controller.Users)

			permissionGroup := userGroup.Group("/permission")
			permissionGroup.GET("/get", controller.Users)
			permissionGroup.POST("/add", controller.Users)
			permissionGroup.PUT("/update", controller.Users)
			permissionGroup.DELETE("/delete", controller.Users)
			permissionGroup.GET("/list", controller.Users)
		}

		emailTplGroup := group.Group("/email/tpl")
		{
			emailTplGroup.GET("/get", controller.Users)
			emailTplGroup.POST("/add", controller.Users)
			emailTplGroup.PUT("/update", controller.Users)
			emailTplGroup.DELETE("/delete", controller.Users)
			emailTplGroup.GET("/list", controller.Users)

			emailTplCategoryGroup := emailTplGroup.Group("/category")
			emailTplCategoryGroup.GET("/get", controller.Users)
			emailTplCategoryGroup.POST("/add", controller.Users)
			emailTplCategoryGroup.PUT("/update", controller.Users)
			emailTplCategoryGroup.DELETE("/delete", controller.Users)
			emailTplCategoryGroup.GET("/list", controller.Users)

		}

		mailServerGroup := group.Group("/mail_server")
		{
			mailServerGroup.GET("/get", controller.Users)
			mailServerGroup.POST("/add", controller.Users)
			mailServerGroup.PUT("/update", controller.Users)
			mailServerGroup.DELETE("/delete", controller.Users)
			mailServerGroup.GET("/list", controller.Users)
		}

		taskGroup := group.Group("/task")
		{
			taskGroup.GET("/get", controller.Users)
			taskGroup.GET("/list", controller.Users)
		}

		sendGroup := group.Group("/send")
		{
			sendGroup.GET("/get", controller.Users)
			sendGroup.GET("/list", controller.Users)
		}
	}

	// for third party applications
	authGroup := r.Group("/api/v1")
	{
		authGroup.POST("/task", controller.Users)
		authGroup.GET("/task", controller.Users)
	}

	// add swagger
	docs.SwaggerInfo.BasePath = "/api"
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	return r
}
